package com.suineg.pms.commons.wabacus;

import java.util.Date;
import java.util.List;
import java.util.Map;


import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.commons.UserHoder;
import com.suineg.pms.entity.User;
import com.suineg.pms.entity.fee.FeePlan;
import com.suineg.pms.entity.fee.FeeScal;
import com.suineg.pms.service.fee.FeePlanService;
import com.suineg.pms.service.fee.FeeScaleService;
import com.suineg.pms.util.DateUtils;
import com.suineg.pms.util.StringUtils;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 * 收费方案保存拦截器
 *@author Suine
 */
public class FeePlanInterceper extends AbsInterceptorDefaultAdapter{
	
	private FeePlanService feePlanService = AppConfig.getInstance().getWac().getBean(FeePlanService.class);
	private FeeScaleService feeScaleService = AppConfig.getInstance().getWac().getBean(FeeScaleService.class);
	
	public boolean check(ReportRequest rrequest,Map<String, String> mRowData) {
		String name = mRowData.get("name");
		String id = mRowData.get("id");
		
		FeePlan feePlan = null;
		
		boolean f = true;
		
		if(StringUtils.isEmpty(id)){		//添加
			feePlan = feePlanService.findByName(name);
			if(feePlan != null){
				rrequest.getWResponse().getMessageCollector().warn("此“方案名: " + name + "”已存在！", false, Consts.STATECODE_FAILED);
				f = false;
			}
		}else{								//修改
			feePlan = feePlanService.get(Long.valueOf(id));
			if(!feePlan.getName().equals(name)){			//未修改方案名
				feePlan = feePlanService.findByName(name);
				if(feePlan != null){
					rrequest.getWResponse().getMessageCollector().warn("此“方案名: " + name + "”已存在！", false, Consts.STATECODE_FAILED);
					f = false;
				}
			}
		}
		return f;
	}

	/**
	 * 保存操作
	 */
	@Override
	public int doSave(ReportRequest rrequest, ReportBean rbean, AbsEditableReportEditDataBean editbean) {
		
		List<Map<String,String>> insertList = rrequest.getLstInsertedData(rbean);
		List<Map<String,String>> updateList = rrequest.getLstUpdatedData(rbean);
		
		User user = new User(UserHoder.getCurrentShiroUser().getId());
		Date now = new Date();
		Map<String,String> map = null;
		
		FeePlan feePlan = null;
		List<FeeScal> scalList = null;
		
		if(insertList != null){					//添加
			map = insertList.get(0);
			if(!check(rrequest,map)){
				return WX_RETURNVAL_SKIP;
			}
			Date startDate = DateUtils.toDate("yyyy-MM-dd", map.get("start_date"));
			Date endDate = DateUtils.toDate("yyyy-MM-dd", map.get("end_date"));;
			String name = map.get("name");
			String ids = map.get("scale_ids");
//			String flag = map.get("flag");
			Long isEnable = Long.valueOf(map.get("is_enable"));
			
			feePlan = new FeePlan(name,startDate,endDate,isEnable,user,now);
			//默认都是公共的
			feePlan.setFlag(1L);
			scalList = feeScaleService.findByIds(ids);
			feePlan.setFeeScalList(scalList);
			
			feePlanService.save(feePlan);
		}else{									//修改
			map = updateList.get(0);
			
			if(!check(rrequest,map)){
				return WX_RETURNVAL_SKIP;
			}
			Date startDate = DateUtils.toDate("yyyy-MM-dd", map.get("start_date"));
			Date endDate = DateUtils.toDate("yyyy-MM-dd", map.get("end_date"));;
			String name = map.get("name");
			String ids = map.get("scale_ids");
			Long isEnable = Long.valueOf(map.get("is_enable"));
			Long id = Long.valueOf(map.get("id"));
			
			feePlan = feePlanService.get(id);
			feePlan.setEndDate(endDate);
			feePlan.setStartDate(startDate);
			feePlan.setIsEnable(isEnable);
			feePlan.setName(name);
			feePlan.setOpdate(now);
			feePlan.setOpuser(user);
			
			scalList = feeScaleService.findByIds(ids);
			feePlan.setFeeScalList(scalList);
			
			feePlanService.save(feePlan);
		}
		return WX_RETURNVAL_SUCCESS;
	}
	
}

